$(function(){
	/*//图标隐藏菜单
	$(".entrance").hover(function(){
		$(this).children(".user-menu").show();
	},function(){
		$(this).children(".user-menu").hide();
	});

	$('.action .detailed').each(function(){
		$(this).click(function() {
        	detailed_content();
        	return false;
        });
  	});

	$('.action .thinkbox-image').each(function(){
		$(this).click(function() {
        	thinkbox_image();
        	return false;
        });
  	});

	(function(){
		var $nav = $("#nav"), $current = $nav.children("[data-key=" + $nav.data("key") + "]");
		if($nav.length){
			$current.addClass("current");
		} else {
			$("#nav").children().first().addClass("current");
		}
	})();*/

	/*显示收藏用户列表*/
	$('.sheet-collect-count').click(function () {
		id = $(this).attr('id');
		$('#user-' + id).toggle();
    });

	/*滚动距离大于100显示返回顶部按钮*/
    $(window).scroll(function () {
        if ($(window).scrollTop() > 100) {
            $(".backToTop").fadeIn(100);
        }
        else {
            $(".backToTop").fadeOut(100);
        }
    });

    /*返回顶部*/
    $('.backToTop').click(function () {
        if ($('html').scrollTop()) {
            $('html').animate({ scrollTop: 0 }, 300);
            return false;
        }
        $('body').animate({ scrollTop: 0 }, 300);
        return false;
    });

    /*default tab action*/
    $(".search-tab").hide(); //Hide all tab-content
    $("#search-menu li:first").addClass("active").show(); //Activate first tab
    $(".search-tab:first").show(); //Show first tab content
    $('.search-count').hide();
    $('.search-count:first').show();

    /*搜索tab切换*/
    $('#search-menu li').click(function () {
        if ($(this).hasClass('active')) {
            return false;
        }
        $('#search-menu li').removeClass('active');
        $(this).addClass('active');


        $('.search-tab').hide(); //hide all
        $('.search-count').hide();
        var activeTab = '#' + $(this).attr('id') + '-tab';
        $(activeTab).fadeIn();

        var activeResCount = '#' + $(this).attr('id') + '-search-count';
        $(activeResCount).fadeIn(100);
    });

    /*点击评论跳转至评论框*/
    $('#comment').on('click', function () {
        $('.comment-textarea').focus();
        var H = $('.comment-textarea').scrollTop();
        console.log()
        $('body').scrollTop(200);
    });


    /*切换曲谱上一页*/
    $('.prev-arrow').on('click', function () {
        $('.sheet-contain .active').prev().addClass('active');
        $('.sheet-contain .active').next().removeClass('active');
        $('.sheet-indicators .active').prev().addClass('active');
        $('.sheet-indicators .active').next().removeClass('active');

    });
    /*切换曲谱下一页*/
    $('.next-arrow').on('click', function () {
        $('.sheet-contain .active').next().addClass('active');
        $('.sheet-contain .active').prev().removeClass('active');
        $('.sheet-indicators .active').next().addClass('active');
        $('.sheet-indicators .active').prev().removeClass('active');

    });

    /*曲谱指标宽度*/
    var w = $('.sheet-indicators').width() / ($('.sheet-indicators li').length * 2 ) - 14;
    $('.sheet-indicators .indicator').css('width', w);

    /*收藏曲谱*/
    collectSheet();

    /*判断用户是否喜欢曲谱*/
    isLikeSheet();

    /*喜欢曲谱*/
    likeSheet();

    /*判断用户是否收藏谱单*/
    isFavScore();

    /*收藏谱单*/
    favScore();
});


/* 限制文字字数 */
function checkLen(obj,maxs){
    var maxChars = maxs;//最多字符数
    if (obj.value.length > maxChars){
        obj.value = obj.value.substring(0,maxChars);
    }
    var curr = obj.value.length;
    $(obj).parents("div").find(".comment-length span").text(curr.toString());
}

/*收藏曲谱*/
function collectSheet() {
    $('.collect-btn').click(function () {
        //验证是否登录
        var url = '/Home/login/check';
        var sheet_id = $(this).attr('data-id');
        $.get(url, function (result) {
            if (result.code == 200) {
                var score_url = '/Home/score/collectSheet/id/'+ sheet_id;
                layer.open({
                    type: 2,
                    title: '收藏到谱单',
                    area: ['480px', '380px'],
                    content: score_url //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                });
            } else if (result.code == 10001) {
                //登录窗口
                layer.open({
                    type: 2,
                    title: '',
                    shadeClose: true,
                    area: ['600px', '420px'],
                    content: '/Home/login/index'
                });
            }
        });
    });
}

/*判断用户是否喜欢曲谱*/
function isLikeSheet() {
    $('.like-btn').each(function () {
        status = $(this).attr('data-status');
        if (status == 1) {
            $(this).addClass('have-like-btn');
            $('.have-like-btn span').hide();
        }
    });
}


/*喜欢曲谱*/
function likeSheet() {
    $('.like-btn').click(function () {
        var now_status = $(this).attr('data-status');
        var id = $(this).attr('data-id');
        var status = 1;
        var url = '/Home/sheet/favSheet';
        var _this = $(this);
        if (now_status == 1) {
            status = 0;
            var data = {"status":status,"sheet_id":id};
            $.post(url, data, function (result) {
                console.log(result.code);
                if (result.code == 200) {
                    _this.attr('data-status', 0);
                    _this.removeClass('have-like-btn');
                    _this.find("span").show();
                } else if (result.code == 10001) {
                    layer.msg(result.msg, {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        } else {
            var data = {"status":status,"sheet_id":id};
            $.post(url, data, function (result) {
                console.log(result.code);
                if (result.code == 200) {
                    _this.attr('data-status', 1);
                    _this.addClass('have-like-btn');
                    _this.find("span").hide();
                } else if (result.code == 10001) {
                    //登录窗口
                    layer.open({
                        type: 2,
                        title: '',
                        area: ['600px', '420px'],
                        content: '/Home/login/index'
                    });
                }
            });
        }
    });
}

/*判断用户是否收藏谱单*/
function isFavScore() {
    var status = $('#collect').attr('data-status');
    if (status == 1) {
        $('#collect').addClass('btn-disable');
        $('#collect .text').html('已收藏');
    }
}

/*收藏谱单*/
function favScore() {
    $('#collect').click(function () {
        var now_status = $(this).attr('data-status');
        console.log(now_status);
        if (now_status == 0) {
            var id = $(this).attr('data-id');
            var status = 1;
            var url = '/Home/score/favScore';
            var _this = $(this);
            var data = {"status":status,"score_id":id};
            $.post(url, data, function (result) {
                console.log(result.code);
                if (result.code == 200) {
                    _this.attr('data-status', 1);
                    _this.addClass('btn-disable');
                    _this.find('.text').html('已收藏');
                } else if (result.code == 10001) {
                    //登录窗口
                    layer.open({
                        type: 2,
                        title: '',
                        area: ['600px', '420px'],
                        content: '/Home/login/index'
                    });
                }
            });
        }
    });
}

/*function search() {
    var style = $('#search-menu .active').attr('id');
    var url = "/Home/search/index.html";

    var data = {
        "name":$('.search-form input').val(),
        "style": style
    };
    console.log(data);
    $.ajax({
        url: url,
        type: "post",
        dataType: 'json',
        data: data,
        success: function (result) {

        }
    });
}*/



